package com.i2works.smartluolong.manage.controller.flow;

import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.FlowTemplateDO;
import com.i2works.smartluolong.web.entity.flow.FlowTemplateQuery;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作流模板控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/28 11:05
 */

@Controller
@RequestMapping("/flow/template")
public class FlowTemplateController extends BaseModule {

    private final IFlowTemplateService flowTemplateService;

    private final IFlowService flowService;

    @Autowired
    public FlowTemplateController(IFlowService flowService, IFlowTemplateService flowTemplateService) {
        this.flowService = flowService;
        this.flowTemplateService = flowTemplateService;
    }


    /**
     * 编辑添加流程模板
     */
    @GetMapping(value = "/save/{flowId}")
    public String savePage(@PathVariable Integer flowId, Model model) {
        FlowTemplateQuery query = new FlowTemplateQuery();
        query.setFlowId(flowId);
        try {
            List<FlowTemplateDO> queryList = flowTemplateService.queryList(query);
            model.addAttribute("flowTemplateList",queryList);
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        logger.info("工作流模板新增");
        return "/flow/template_save";
    }


    /**
     * 编辑添加流程模板
     */
    @PostMapping("/save/action")
    @ResponseBody
    public Result saveTemplate(@RequestBody List<FlowTemplateDO> flowTemplateList) {
        if (CollectionUtil.isEmpty(flowTemplateList)) {
            return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
        }
        try {
            flowTemplateService.saveBatch(flowTemplateList,getUserName());
        } catch (ServiceException e) {
            logger.error(e.getMessage());
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
        return R.success();
    }



}
